/***************************************************************
 * Name:      Ex1Main.cpp
 * Purpose:   Code for Application Frame
 * Author:    Edward ()
 * Created:   2012-05-16
 * Copyright: Edward ()
 * License:
 **************************************************************/

#include "Ex1Main.h"
#include <wx/msgdlg.h>
#include <wx/dcclient.h>
//GDAL header
#include <gdal_priv.h>
#include "cpl_conv.h"
#include <gdal.h>
//(*InternalHeaders(Ex1Frame)
#include <wx/artprov.h>
#include <wx/bitmap.h>
#include <wx/intl.h>
#include <wx/image.h>
#include <wx/string.h>
//*)

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*IdInit(Ex1Frame)
const long Ex1Frame::idMenuQuit = wxNewId();
const long Ex1Frame::idMenuAbout = wxNewId();
const long Ex1Frame::ID_STATUSBAR1 = wxNewId();
const long Ex1Frame::ID_TOOLBARITEM1 = wxNewId();
const long Ex1Frame::ID_TOOLBAR1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(Ex1Frame,wxFrame)
    //(*EventTable(Ex1Frame)
    //*)
END_EVENT_TABLE()

Ex1Frame::Ex1Frame(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(Ex1Frame)
    wxMenuItem* MenuItem2;
    wxMenuItem* MenuItem1;
    wxMenu* Menu1;
    wxMenuBar* MenuBar1;
    wxMenu* Menu2;

    Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
    MenuBar1 = new wxMenuBar();
    Menu1 = new wxMenu();
    MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
    Menu1->Append(MenuItem1);
    MenuBar1->Append(Menu1, _("&File"));
    Menu2 = new wxMenu();
    MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
    Menu2->Append(MenuItem2);
    MenuBar1->Append(Menu2, _("Help"));
    SetMenuBar(MenuBar1);
    StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
    int __wxStatusBarWidths_1[1] = { -1 };
    int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
    StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1);
    StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1);
    SetStatusBar(StatusBar1);
    ToolBar1 = new wxToolBar(this, ID_TOOLBAR1, wxDefaultPosition, wxDefaultSize, wxTB_HORIZONTAL|wxNO_BORDER, _T("ID_TOOLBAR1"));
    ToolBarItem1 = ToolBar1->AddTool(ID_TOOLBARITEM1, _("New item"), wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(_T("wxART_CROSS_MARK")),wxART_TOOLBAR), wxNullBitmap, wxITEM_NORMAL, wxEmptyString, wxEmptyString);
    ToolBar1->Realize();
    SetToolBar(ToolBar1);

    Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&Ex1Frame::OnQuit);
    Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&Ex1Frame::OnAbout);
    Connect(ID_TOOLBARITEM1,wxEVT_COMMAND_TOOL_CLICKED,(wxObjectEventFunction)&Ex1Frame::OnToolBarItem1Clicked);
    Connect(wxEVT_PAINT,(wxObjectEventFunction)&Ex1Frame::OnPaint);
    //*)
}

Ex1Frame::~Ex1Frame()
{
    //(*Destroy(Ex1Frame)
    //*)
}

void Ex1Frame::OnQuit(wxCommandEvent& event)
{
    Close();
}

void Ex1Frame::OnAbout(wxCommandEvent& event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}

void Ex1Frame::OnPaint(wxPaintEvent& event)
{
    wxPaintDC dc(this);
    dc.DrawText(wxT("Testing"), 40, 60);
    wxPoint pts[3];
    pts[0] = wxPoint(10,10);
    pts[1] = wxPoint(100,100);
    pts[2] = wxPoint(300,500);
    dc.DrawLines(3,pts,0,0);
    wxBrush brush(*wxRED,wxFDIAGONAL_HATCH);
    dc.SetBrush(brush);
    dc.DrawText(wxT("Testing"), 40, 60);
    wxPoint star[5];
    star[0]= wxPoint(100,60);
    star[1]= wxPoint(60,150);
    star[2] = wxPoint(160, 100);
    star[3] = wxPoint(40, 100);
    star[4] = wxPoint(140, 150);
    dc.DrawPolygon(WXSIZEOF(star),star,0,30);
    dc.DrawPolygon(WXSIZEOF(star),star,160,30,wxWINDING_RULE);

}


void Ex1Frame::OnToolBarItem1Clicked(wxCommandEvent& event)
{

    wxClientDC dc(this);
    wxPoint ptOrigin = this->GetClientAreaOrigin();
    //wxMessageBox(wxT("clicked"));
    wxString s = wxString::Format(wxT("%d"),ptOrigin.x+1);
    wxMessageBox(s);

    //dc.Blit(0,0,1000,500,&dc,100,0,wxCOPY);
    //dc1.Blit()
    //dc.Blit()
//    GDALDataset  *poDataset;
//    GDALAllRegister();
    GDALDatasetH  hDataset;
    //hDataset = GDALOpen( ("data"), GA_ReadOnly );
    wxPen * pPen = new wxPen();
    pPen->SetColour(wxColor(255,0,0));

    CPoint pt(10.3,12.2);

    DrawPoint(pt);
    delete pPen;

}
void Ex1Frame::DrawPoint(const CPoint& pt)
{
    wxClientDC dc(this);
    //dc.DrawPoint(pt.GetX(),pt.GetY());
    dc.DrawPoint(10,50);

}
